package com.c.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.c.entity.Address;
import com.c.service.AddressService;
import com.c.service.UserService;
import com.c.utils.MessageConstant;
import com.c.utils.PageResult;
import com.c.utils.QueryPageBean;
import com.c.utils.Result;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @author wangxiangli
 * @create 2022-12-26-15:23
 */
@RestController
@RequestMapping("/map")
public class AddressController {

    @Autowired
    private AddressService addressService;


    // 地图数据的分布查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        return addressService.pageQuery(queryPageBean);
    }


    /**
     * 获取公司地址的详细信息
     *
     * @return
     */
    @PreAuthorize("hasAuthority('ADDRESS_QUERY')")
    @RequestMapping("/getAddressDetail")
    public Result getAddressDetail() {
        try {
            List<Address> list = addressService.list();
            return new Result(true, MessageConstant.GET_ADDRESS_SUCCESS, list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_ADDRESS_FAIL);
        }
    }

    /**
     * 添加地址
     *
     * @param address
     * @return
     */
    @PreAuthorize("hasAuthority('ADDRESS_ADD')")
    @RequestMapping("add")
    public Result add(@RequestBody Address address) {
        try {
            addressService.save(address);
            return new Result(true, MessageConstant.ADD_ADDRESS_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_ADDRESS_FAIL);
        }
    }

    /**
     * 删除地址
     *
     * @param id
     * @return
     */
    @PreAuthorize("hasAuthority('ADDRESS_DELETE')")
    @RequestMapping("/delete")
    public Result delete(Integer id) {
        try {
            addressService.removeById(id);
            return new Result(true, MessageConstant.DELETE_ADDRESS_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_ADDRESS_FAIL);
        }
    }

}
